<script setup>
import {inject, provide, reactive, ref} from "vue";
import NrDatePicker from "../common/nr/NrDatePicker.vue";
import RealtimeJob from "realtime-job-executor";
import {useMessage} from "jiuqi-ui";
import CheckInfo from "./CheckInfo.vue";
import {Split} from "view-ui-plus";
import {getOffsetDate} from "../../utils/index.js"
import IconEnum from "../../utils/iconEnum";
import UnitSelector from "./unitselector/UnitSelector.vue";

//================================================【公用相关】============================================================
//后端请求方法
const context = inject('context')
//消息组件
const message = useMessage()
//时期参数
const periodTypeCode = context.getParam().periodTypeCode
const periodOffset = context.getParam().periodOffset

const dateTime = window.iviewex.dataUtil.dateToStr({
    date: getOffsetDate(periodTypeCode, periodOffset),
    type: (periodTypeCode || 'R')
})

//分摊审核请求参数（同步 时期参数、单位参数）
const dataCheckReqParam = reactive({nrUnitCode: '', nrUnitName: '', nrDateParam: dateTime})

//当前 分摊审核 结果类型
const dataCheckResType = ref('')

//面板分割参数
const split = ref(0.15)

//重新审核按钮 失效控制
const reCheckDisable = ref(true)
//导出按钮 失效控制
const exportDisable = ref(true)
//================================================【组件属性】============================================================

//================================================【组件通信】============================================================
provide('reCheckDisable', reCheckDisable)
provide('exportDisable', exportDisable)

//分摊审核请求参数（同步 时期参数、单位参数）
provide('dataCheckReqParam', dataCheckReqParam)
//分摊审核结果类型
provide('dataCheckResType', dataCheckResType)

//================================================【普通方法】============================================================
//==================================================================【时期控件--编辑时间】
function onDateChange(dataObj) {
    dataCheckReqParam.nrDateParam = dataObj.data.date
}

//==================================================================【审核结果导出】
const dateStr = ref('')

function dataOutput() {
    context.getPlugin('http').post(`abc/dataValidator/export/${dataCheckReqParam.nrDateParam}/${dataCheckReqParam.nrUnitCode}`, {}, {
        headers: {
            // 'Content-Type': 'application/octet-stream',
            'x-requested-with': "XMLHttpRequest",
            // eslint-disable-next-line no-undef
            'Authorization': context.getPlugin('http').getConfig().headers.Authorization
        },
        responseType: 'blob'
    }).then(async res => {
        //获取翻译后的时期参数
        await context.getPlugin('http').get(`abc/model/info/parsedDateStr/${dataCheckReqParam.nrDateParam}`).then(async res => {
            dateStr.value = res.data
        })
        const blob = new Blob([res.data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
        const url = window.URL.createObjectURL(blob)
        const aLink = document.createElement('a')
        aLink.style.display = 'none'
        aLink.href = url
        aLink.setAttribute('download', '数据审核-' + dateStr.value + '-' + dataCheckReqParam.nrUnitName + '.xlsx')
        document.body.appendChild(aLink)
        aLink.click()
        document.body.removeChild(aLink) // 下载完成移除元素
        window.URL.revokeObjectURL(url) // 释放掉blob对象
    }).catch(() => {
        message.error("导出失败")
    })
}

//==================================================================【重新计算并审核】
//子组件实例
const sonCheckInfo = ref(null)

async function reCalculateAndCheck() {

    await new RealtimeJob().post({
        group: "com.jiuqi.abc.calculate.realtimeJob",
        title: "执行分摊",
        params: {
            'periodList': dataCheckReqParam.nrDateParam,
            'unitCodeList': dataCheckReqParam.nrUnitCode
        },
        linkSources: [],
        submitCallback: async (jobGuid) => {
            //调用子组件轮询方法
            sonCheckInfo.value.pollingProgress(jobGuid)

        }
    })
}

//================================================【生命周期】============================================================
</script>

<template>
    <div style="width: 100%;height: 100%">
        <j-layout class="layout">
            <!--<editor-fold desc="头部菜单栏">-->
            <j-header class="toolbar">
                <div class="btnSContainer">
                    <j-space size="large" style="height: 48px;line-height: 48px;overflow: hidden !important;gap:0px;">
                        <span style="width: 48px">时期：</span>
                        <NrDatePicker
                                style="line-height: 16px"
                                :date="dataCheckReqParam.nrDateParam"
                                :inputSlot="false"
                                @datechange="onDateChange"
                        ></NrDatePicker>
                    </j-space>
                    <j-space :size="16" style="width: 216px">
                        <j-button @click="reCalculateAndCheck" :disabled="reCheckDisable"
                                  style="width: 120px;height: 32px">重新计算并审核
                        </j-button>
                        <j-button @click="dataOutput" :disabled="exportDisable" style="width: 48px;height: 32px">导出
                        </j-button>
                    </j-space>
                </div>
            </j-header>
            <!--</editor-fold>-->

            <!--<editor-fold desc="页面主体内容">-->
            <j-content class="contentContainer">
                <Split v-model="split" :min="0.1" max="800">
                    <template #left>
                        <j-content class="leftUnit">
                          <UnitSelector
                              :nrDateParam="dataCheckReqParam.nrDateParam"
                              :dataCheckResType="dataCheckResType"
                              v-model:nrUnitCode="dataCheckReqParam.nrUnitCode"
                              v-model:nrUnitName="dataCheckReqParam.nrUnitName">
                            <template #dropDownItem="{item}">
                              <j-space :size="8">
                                <div v-if="item.validateStatusEnum === 'SUCCESS'" style="padding-left: 2px">
                                  <j-badge dot type="success" :offset="[22, 17]">
                                    <span :class=" 'nvwa-iconfont' + (item.hasChildren ?  IconEnum.notLeaf : IconEnum.isLeaf )"></span>
                                  </j-badge>
                                </div>
                                <div v-else-if="item.validateStatusEnum === 'ERROR'" style="padding-left: 2px">
                                  <j-badge dot type="error" :offset="[22, 17]">
                                    <span :class=" 'nvwa-iconfont' + (item.hasChildren ?  IconEnum.notLeaf : IconEnum.isLeaf )"></span>
                                  </j-badge>
                                </div>
                                <div v-else-if="item.validateStatusEnum === 'WARNING'" style="padding-left: 2px">
                                  <j-badge dot type="warning" :offset="[22, 17]">
                                    <span :class=" 'nvwa-iconfont' + (item.hasChildren ?  IconEnum.notLeaf : IconEnum.isLeaf )"></span>
                                  </j-badge>
                                </div>
                                <div v-else-if="item.validateStatusEnum === 'PROCESS'" style="padding-left: 2px">
                                  <j-badge dot type="process" :offset="[22, 17]">
                                    <span :class=" 'nvwa-iconfont' + (item.hasChildren ?  IconEnum.notLeaf : IconEnum.isLeaf )"></span>
                                  </j-badge>
                                </div>
                                <div v-else-if="item.validateStatusEnum === 'OTHER'" style="padding-left: 2px">
                                        <span :class=" 'nvwa-iconfont'
                                                        +
                                                        (item.hasChildren ?  IconEnum.notLeaf : IconEnum.isLeaf )
                                                        +
                                                        (item.hasModel ? '' : ' nonModelFontClass' )">
                                        </span>
                                </div>
                                <span :class="item.hasModel  ? '' : ' nonModelFontClass'">{{ item.code + ' | ' + item.name }}</span>
                              </j-space>
                            </template>
                            <template #treeNode="{node}">
                              <j-space :size="8">
                                <div v-if="node.validateStatusEnum === 'SUCCESS'">
                                  <j-badge dot type="success" :offset="[22, 17]">
                                        <span :class=" 'nvwa-iconfont'
                                                        +
                                                        (node.isLeaf ? IconEnum.isLeaf : IconEnum.notLeaf )">
                                        </span>
                                  </j-badge>
                                </div>
                                <div v-else-if="node.validateStatusEnum === 'ERROR'">
                                  <j-badge dot type="error" :offset="[22, 17]">
                                        <span :class=" 'nvwa-iconfont'
                                                        +
                                                        (node.isLeaf ? IconEnum.isLeaf : IconEnum.notLeaf )">
                                        </span>
                                  </j-badge>
                                </div>
                                <div v-else-if="node.validateStatusEnum === 'WARNING'">
                                  <j-badge dot type="warning" :offset="[22, 17]">
                                        <span :class=" 'nvwa-iconfont'
                                                        +
                                                        (node.isLeaf ? IconEnum.isLeaf : IconEnum.notLeaf )">
                                        </span>
                                  </j-badge>
                                </div>
                                <div v-else-if="node.validateStatusEnum === 'PROCESS'">
                                  <j-badge dot type="process" :offset="[22, 17]">
                                        <span :class=" 'nvwa-iconfont'
                                                        +
                                                        (node.isLeaf ? IconEnum.isLeaf : IconEnum.notLeaf )">
                                        </span>
                                  </j-badge>
                                </div>
                                <div v-else-if="node.validateStatusEnum === 'OTHER'">
                                    <span :class=" 'nvwa-iconfont'
                                                    +
                                                    (node.isLeaf ? IconEnum.isLeaf : IconEnum.notLeaf )
                                                    +
                                                    (node.hasModel ? '' : ' nonModelFontClass' )">
                                    </span>
                                </div>
                                <span :class="node.hasModel  ? '' : ' nonModelFontClass'
                                              +
                                              (node.selected ?' treeScrollIntoViewTarget' : '')">
                                    {{ node.id }} | {{ node.title }}
                                </span>
                              </j-space>
                            </template>
                            <template #tableRowStatus="{row}">
                              <div v-if="row.validateStatusEnum === 'SUCCESS'">
                                <j-badge dot type="success" :offset="[22, 17]">
                                    <span :class=" 'nvwa-iconfont'
                                                    +
                                                    (row.hasChildren ? IconEnum.notLeaf : IconEnum.isLeaf)
                                                    +
                                                    (row.highlight ? ' tableScrollIntoViewTarget' : '')">
                                    </span>
                                </j-badge>
                              </div>
                              <div v-else-if="row.validateStatusEnum === 'ERROR'">
                                <j-badge dot type="error" :offset="[22, 17]">
                                    <span :class=" 'nvwa-iconfont'
                                                    +
                                                    (row.hasChildren ? IconEnum.notLeaf : IconEnum.isLeaf)
                                                    +
                                                    (row.highlight ? ' tableScrollIntoViewTarget' : '')">
                                    </span>
                                </j-badge>
                              </div>
                              <div v-else-if="row.validateStatusEnum === 'WARNING'">
                                <j-badge dot type="warning" :offset="[22, 17]">
                                    <span :class=" 'nvwa-iconfont'
                                                    +
                                                    (row.hasChildren ?  IconEnum.notLeaf : IconEnum.isLeaf)
                                                    +
                                                    (row.highlight ? ' tableScrollIntoViewTarget' : '')">
                                    </span>
                                </j-badge>
                              </div>
                              <div v-else-if="row.validateStatusEnum === 'PROCESS'">
                                <j-badge dot type="process" :offset="[22, 17]">
                                    <span :class=" 'nvwa-iconfont'
                                                    +
                                                    (row.hasChildren ?  IconEnum.notLeaf : IconEnum.isLeaf)
                                                    +
                                                    (row.highlight ? ' tableScrollIntoViewTarget' : '')">
                                    </span>
                                </j-badge>
                              </div>
                              <div v-else-if="row.validateStatusEnum === 'OTHER'">
                                        <span :class=" 'nvwa-iconfont'
                                                        +
                                                        (row.hasChildren ? IconEnum.notLeaf : IconEnum.isLeaf)
                                                        +
                                                        (row.hasModel ? '' : ' nonModelFontClass' )
                                                        +
                                                        (row.highlight ? ' tableScrollIntoViewTarget' : '')">
                                        </span>
                              </div>
                            </template>
                          </UnitSelector>
                        </j-content>
                    </template>
                    <template #right>
                        <j-content class="rightInfo">
                            <CheckInfo ref="sonCheckInfo"></CheckInfo>
                        </j-content>
                    </template>
                </Split>
            </j-content>
            <!--</editor-fold>-->
        </j-layout>
    </div>
</template>

<style scoped>
.layout {
    background: #ffffff;
    position: relative;
    overflow: hidden;
}

.toolbar {
    height: 49px !important;
    line-height: 49px;
    border-bottom: 1px solid #ccc;
    padding-left: 8px;
    padding-right: 16px;
}

.btnSContainer {
    display: flex !important;
    flex-direction: row;
    justify-content: space-between;
}

.contentContainer {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    height: 100%;
}

.leftUnit {
    width: 100%;
    height: 100%;
}
.nonModelFontClass {
  color: #c1c1c1;
}
.rightInfo {
    width: 100%;
    height: 100%;
    padding-left: 13px;
    overflow-y: auto;
}
</style>
